

    const userService = require('../service/userService.js')
    const encryption = require('../lib/encryption')

    module.exports = {

        loginGetToken: async (ctx, next) => {
            const username = 'jack'
            const password = '654321'
            if(username && password) {
                let user = await userService.loginGetToken(username, password)
                if(user) {
                    let payload =  {username: user.username}
                    let token = encryption.getUserToken(payload)
                    if(token) {
                        ctx.request.header.Authorization = token
                        ctx.body = {
                            success: true,
                            err: { code: 1, msg: '获取认证成功！' },
                            data: {
                                token: token
                            }
                        }
                    }
                    else {
                        ctx.body = { success: false, err: { code: 0, msg: '获取认证失败！' } }
                    }
                }
                else {
                    ctx.body = { success: false, err: { code: 0, msg: '用户不存在！' } }
                }
            }
            else {
                ctx.body = { success: false, err: { code: 0, msg: '账号密码提交失败！' } }
            }
        },

        getUserInfoById: async (ctx, next) => {
            let userinfo = await userService.getUserInfoById(1)
            ctx.body = {
                userinfo: userinfo
            }
        }

    }